package com.heo.music.player.dto.query;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * @author 星天无极
 **/
@Data
@ApiModel(description = "分页信息")
@AllArgsConstructor
@NoArgsConstructor
public class PageQuery {

    @Min(value = 1, message = "页码不能小于0")
    @Max(value = Integer.MAX_VALUE, message = "页码超过最大值")
    @ApiModelProperty(value = "页码", example = "1", position = 1000)
    private Integer page;

    @Min(value = 1, message = "size不能小于1")
    @Max(value = 500, message = "size不能大于500")
    @ApiModelProperty(value = "页数", example = "10", position = 1001)
    private Integer size;


    /**
     * @return page
     */
    public int getPage() {
        if (page == null) {
            return 1;
        }
        return page;
    }

    public int getSize() {
        if (size == null) {
            size = 500;
        }
        return size;
    }

    public Pageable convertPageable() {
        int page = this.getPage() - 1;
        int size = this.getSize();
        return PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createTime"));
    }


}
